﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Deployment.Application;

namespace ModernControls
{
    /// <summary>
    /// Interaction logic for WindowHeader.xaml
    /// </summary>
    public partial class WindowHeader : UserControl, INotifyPropertyChanged
    {
        #region properties

        private string version;

        public string Version
        {
            get { return version; }
            set { version = value; RaisePropertyChanged("Version"); }
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(WindowHeader), new UIPropertyMetadata("", new PropertyChangedCallback(
                (sender, args) =>
                {
                    if (sender != null)
                    {
                        (sender as WindowHeader).titleTextBlock.Text = args.NewValue as string;
                    }
                })));


        private bool _isToolWindow;

        public bool IsToolWindow
        {
            get { return _isToolWindow; }
            set
            {
                if (value == true)
                {
                    //logoImage.Visibility = System.Windows.Visibility.Collapsed;
                    minimizeButton.Visibility = System.Windows.Visibility.Collapsed;
                    windowStateButton.Visibility = System.Windows.Visibility.Collapsed;
                    chromeRec.Height = 30;
                }
                else
                {
                    //logoImage.Visibility = System.Windows.Visibility.Visible;
                    minimizeButton.Visibility = System.Windows.Visibility.Visible;
                    windowStateButton.Visibility = System.Windows.Visibility.Visible;
                    chromeRec.Height = 55;
                }
                _isToolWindow = value;
            }
        }



        #endregion

        Window hostWindow;

        public WindowHeader()
        {
            InitializeComponent();
            this.Loaded +=
                (s, args) =>
                {
                    this.GetMainWindow();
                };

            DataContext = this;

            try
            {
                ApplicationDeployment deployment = ApplicationDeployment.CurrentDeployment;
                Version thisAppsVersion = deployment.CurrentVersion;
                Version = thisAppsVersion.ToString();
            }
            catch (InvalidDeploymentException)
            { }
        }

        private void GetMainWindow()
        {
            hostWindow = Window.GetWindow(this);
            if (hostWindow != null)
            {
                hostWindow.MinWidth = this.MinWidth;
                hostWindow.ContentRendered +=
                    (s, args) =>
                    {
                        (s as Window).Width++;
                        (s as Window).Width--;
                    };
            }

        }

        void windowStateButton_Click(object sender, RoutedEventArgs e)
        {
            this.ThrowWindowSateClicked(this, new RoutedEventArgs());
            if (hostWindow.WindowState == System.Windows.WindowState.Maximized)
            {
                //rootGrid.Margin = new Thickness(10);
                hostWindow.WindowState = System.Windows.WindowState.Normal;
            }
            else
            {
                //rootGrid.Margin = new Thickness(5);
                hostWindow.WindowState = System.Windows.WindowState.Maximized;
            }
        }

        void minimizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.ThrowMinimizeClicked(this, new RoutedEventArgs());
            hostWindow.WindowState = System.Windows.WindowState.Minimized;
        }


        private void exitButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.ThrowExitClicked(this))
            {
                hostWindow.Close();
            }
            //
        }

        #region events
        public delegate bool ExitClickedEventDelegate();
        public event ExitClickedEventDelegate ExitClicked;
        private bool ThrowExitClicked(object sender)
        {
            if (ExitClicked != null)
            {
                return this.ExitClicked();
            }
            return true;
        }

        public event RoutedEventHandler MinimizeClicked;
        private void ThrowMinimizeClicked(object sender, RoutedEventArgs e)
        {
            if (MinimizeClicked != null)
            {
                MinimizeClicked(sender, e);
            }
        }

        public event RoutedEventHandler WindowStateClicked;
        private void ThrowWindowSateClicked(object sender, RoutedEventArgs e)
        {
            if (WindowStateClicked != null)
            {
                WindowStateClicked(sender, e);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (hostWindow == null)
            {
                this.GetMainWindow();
            }
            if (e.ChangedButton == MouseButton.Left)
            {
                hostWindow.DragMove();
            }
            
        }


    }
}
